import Vue from 'vue'
import Vuex from 'vuex'
import request from '@/utils/request.js'
import {
	baseUrl
} from '@/config/evn.js'
Vue.use(Vuex)
export default new Vuex.Store({
	state: {
		token: '',
		navigationBarHeight: {
			"statusBar": "", //状态栏高度
			"customBar": "", // 状态栏高度 + 导航栏高度 
			"navbar": "" // 自定义标题与胶囊对齐高度
		},
		dictionaryEntry: [], //字典项
	},
	mutations: {
		// 存储token
		setToken(state, token) {
			state.token = token
			uni.setStorageSync("token", token)
		},
		// 获取状态栏高度
		getNavigationBarHeight(state, navigationBarHeight) {
			state.navigationBarHeight = {
				...navigationBarHeight
			}
		},
		// 获取字典
		getDictionaryEntry(state, dictionary) {
			state.dictionaryEntry = {
				...dictionary
			}
			uni.setStorageSync('dictionaryEntry', JSON.stringify(dictionary));
		}
	},
	actions: {
		async asyncDictionaryEntry(context) {
			const res = await request({
				method: 'get',
				url: baseUrl + 'api/system/dict/dictlist'
			});
			// 获取字典项有哪些  获取字典项
			const dictionaryEntryValue = uni.getStorageSync('dictionaryEntry') ? JSON.parse(uni
				.getStorageSync('dictionaryEntry')) : {};
			let dictionaryEntry = res.code === 200 ? [...res.rows] : [
				...dictionaryEntryValue
			];
			context.commit('getDictionaryEntry', dictionaryEntry)
		}
	},
	getters: {
		// 获取字典单个项的数据
		getDictionaryItem: (state) => (item) => {
			const dictionaryEntry = uni.getStorageSync('dictionaryEntry') ? JSON.parse(uni.getStorageSync(
				'dictionaryEntry')) : (state.dictionaryEntry || [])
			if (!Array.isArray(dictionaryEntry)) return []
			const arr = dictionaryEntry.filter(val => val.dictType === item) || []
			arr.forEach(val => {
				val.text = val.dictLabel
				val.value = val.dictValue
			})
			return arr
		}
	},
	modules: {}
})